**************************
***	RUN USING STATA 16 ***
**************************

* Purpose: IRT analysis for all Maths subjective items. Outputs parameters.
* Last Updated: 06 May 2021

*----------------------------------------------------------------------------------------------
* Part 1 MATHS constructed-response IRT
*----------------------------------------------------------------------------------------------
	use "$output\mat_unique.dta", clear	
	
	//first, drop observations for which we only have multiple-choice responses
	drop if set==2
		
	//keep only what needed for now
	keep booklet pid s_*
	
	//categorise into 2-point groups
	forvalues l = 1/10 {
	forvalues m = 1/15 {
		local n : display %02.0f `m'
		cap gen s`l'`n' = round(s_`l'`n'/2, 1)
		cap replace s`l'`n' = .5 if s_`l'`n' ==1
		}
	}
	
	drop s_*
	ren s* s_*
	
	*drop item s_808 (this item omitted)
	drop s_808

*------------------------------------------------------------------------
*------------------------------------------------------------------------
	//extract the list of response categories for each constructed response variable
	preserve
	
		foreach v of varlist s_* {
			tab `v', matrow(cat`v')
			svmat cat`v'
		}
		
		keep cats*
		egen rowtot = rowtotal(cats*), missing
		drop if rowtot==.
		drop rowtot
		
		gen y=_n-1
		
		reshape long cat, i(y) j(j) string
		ren cat b
		ren y value
		ren j item
		replace item = substr(item, 1, length(item) - 1)
		
		drop if value==.
		reshape wide b, i(item) j(value)
		
		*remove TIMSS
		gen length=length(item)
		drop if length==6
		drop length
		
		//rename as value
		ren b* v*	
		
		tempfile values
		save `values'
		
	restore
*------------------------------------------------------------------------
*------------------------------------------------------------------------
	
	cd "$graph\icc\mat" //set icc directory

	uirt s*, gpcm(s_*) icc icc_format(gph) //run irt, output icc for each item
	
	log close
	
	//extract all item parameters
	matrix c = e(item_par) 
	svmat c
	
	//add names of variables alongside parameters
	gen item = ""
	local y=0
	foreach var of varlist s_* {
		local y = `y' + 1
		replace item = "`var'" in `y'
	}
	
	keep c1-item
	keep if c1!=.
	order item, first
	
	ren c1 a
	ren c2 b1
	ren c3 b2
	ren c4 b3
	ren c5 b4
	ren c6 b5
	ren c7 b6
	ren c8 b7
		
	egen options = rownonmiss(b1- b7)
	
	gen x=_n
	
	merge 1:1 item using `values'
	
	drop _merge
	
	//save item parameters
	save "$output\mat_sub_par.dta", replace


	
